<?php
/**
 * @package View
 * @subpackage Widgets
 * @category System
 * @author Dan Krasilnikov <dkrasilnikov@gmail.com>
 * @copyright Copyright (c) 2009, Dan Krasilnikov
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version 0.0.1 alpha  
*/

/**
 * @ignore
 */
require_once 'widgets/areas/TAreasWidget.inc';

/**
 * @package View
 * @subpackage Widgets
 * @category System
 * command bar
 */
class TPageAssocWidget extends TAreasWidget {
	private $_search_condition_ = null;
	
	private $_assoc_class_ = null;
	
	private $_assoc_class_item_ = null;

/**
 * @var TCMSPageAssociationClassModel
 */	
	protected $_ioc_association_classes_model_;
	
	protected function checkSource($src){
		return $src instanceof TCMSPageAssociationModel;
	}	
	
	protected function getPageItem(){
		return $this->response->ModelItem;
	}
	
	protected function getPageModel(){
		return $this->response->Model;
	}
	
	public function http_Find($search){
		if ($search){
			$this->_search_condition_ = $search;
			$this->refreshArea(new TWidgetArea($this, $this, 'Suggestions'));
		}
	}
	
	public function http_Link($link = null){
		if (is_array($link) && $this->AssocClass()){
			$this->Source->AssociatePages($this->AssocClass(),$this->getPageItem(), array_keys($link));
			$this->refreshArea(new TWidgetArea($this, $this, 'Assignments'));
		}
	}
	
	public function http_Unlink($link = null){
		if (is_array($link) && $this->AssocClass()){
			$this->Source->DeassociatePages($this->AssocClass(),$this->getPageItem(), array_keys($link));
			$this->refreshArea(new TWidgetArea($this, $this, 'Assignments'));
		}
	}
	
	private function _sess_storage_name(){
		return $this->response->Name().':'.$this->Name().':assoc_class';
	}
	
	public function http_SetClass($assoc_class){
		$this->Application()->Session()->Set($this->_sess_storage_name(),$assoc_class);
		$this->_assoc_class_item_ = null;
		$this->_assoc_class_ = $assoc_class;
		$this->refreshArea(new TWidgetArea($this, $this, 'Assignments'));
	}
	
	public function AssocClass(){
		if (is_null($this->_assoc_class_item_)){
			if (is_null($this->_assoc_class_)){
				$this->_assoc_class_ = $this->Application()->Session()->Get($this->_sess_storage_name());
				if (!$this->_assoc_class_){
					if ($pi = $this->getPageItem())
						foreach ($pi->AssociationClasses as $c){
							$this->_assoc_class_ = $c->ItemId();
							break;
						}
						
				}
				if (!$this->_assoc_class_){
					$acs = $this->AssociationClassesModel->GetList();
					foreach ($acs as $c){
						$this->_assoc_class_ = $c->ItemId();
						break;
					}					
				}
			}
			
			if ($this->_assoc_class_){
				$this->Application()->Session()->Set($this->_sess_storage_name(),$this->_assoc_class_);
				$this->_assoc_class_item_ = $this->AssociationClassesModel->GetItem($this->_assoc_class_);
			} else
				$this->_assoc_class_item_ = false;
		}
		return $this->_assoc_class_item_;
	}
	
/**
 * gest presenter actions
 * @return array of TPresenterAction
 */
	public function Suggestions(){
		if ($this->_search_condition_)
			return $this->getPageModel()->GetList(new TListOptions(array(new TFilterOption('Title', TConditionType::C_LIKE, $this->_search_condition_))));
		return new TArrayIterator(array());	
	}
	
/**
 * gest presenter actions
 * @return array of TPresenterAction
 */
	public function Associations(){
		if ($this->AssocClass())
			return $this->getPageItem()->Associations($this->AssocClass());
		return new TArrayIterator(array());
	}
	
/*	protected function areaCssClass(TWidgetArea $area){
		$context = $area->;
		if (is_object($context))
			return get_class($context);
		return null;
	}*/
	
	public function Css(){
		?>
			div.page-assoc {
				border-style:inset;
				border-width:1px;
				padding:5px;
			}
			
			div.page-assoc form {
				border-style:none;
				margin:0px;
				padding:0px;
				margin-bottom:10px;
			}
			
			div.page-assoc div.search-results, div.page-assoc div.assignments {
				height:100px;
				overflow:auto;
				border-style:inset;
				border-width:1px;
			}
			
			div.page-assoc form table {
				font-size:9pt;
			}
			
			div.page-assoc a {
				text-decoration:none;
				color:black;
			}
			
			div.page-assoc a:hover {
				text-decoration:underline;
			}
			
			div.page-assoc div.btn-holder {
				text-align:right;
			}
			
			div.page-assoc div.btn-holder input[type=submit]{
				font-size:12pt;
				padding:4px;
			}
		<?php
	}
	
	protected function areaCssClass(TWidgetArea $area){
		if ($area->Skin() == 'Assignments')
			return 'assignments';
		return parent::areaCssClass($area);
	}
	
	protected function renderSuggestions(TWidgetArea $area){
		$list = $this->Suggestions();
		if ($list->ItemCount() > 0) { ?>
		<div class="search-results">
		<table>
<?php		foreach ($list as $p){ ?>
				<tr class="page"><td><input type="checkbox" name="link[<?php echo $p->ItemId(); ?>]" /></td><td><a href="<?php echo $this->response->ItemEditUrl($p); ?>"><?php echo $p; ?></a></td></tr>
<?php		} ?>
		</table>
		</div>
		<div class="btn-holder"><input type="submit" value="<?php __i('Link'); ?>" /></div>
<?php
		}
	}
	
	protected function renderAssignments(TWidgetArea $area){
		$list = $this->Associations();
?>
<table>
<?php		
		foreach ($list as $p){
?>
<tr class="page"><td><input type="checkbox" name="link[<?php echo $p->ItemId(); ?>]" /></td><td><a href="<?php echo $this->response->ItemEditUrl($p); ?>"><?php echo $p; ?></a></td></tr>
<?php			
		}
?>
</table>
<?php		
	}

/**
 * @see TWidget::DefaultOutput()
 */	
	protected function defaultOutput(IThemeEngine $engine){
		if ($this->getPageItem()){
?>
<div id="<?php echo $this->Name() ?>" class="page-assoc">
<form id="<?php echo $this->Name() ?>_search" method="post" action="<?php $this->response->Url(); ?>">
	<input type="hidden" name="controller" value="<?php echo $this->Name(); ?>" />
	<input type="hidden" name="method" value="Find" />
	<input type="text" name="search" value="" />
	<input type="submit" value="<?php __i('Find'); ?>" />
</form>
<form id="<?php echo $this->Name() ?>_assocclass" method="post" action="<?php $this->response->Url(); ?>">
	<input type="hidden" name="controller" value="<?php echo $this->Name(); ?>" />
	<input type="hidden" name="method" value="SetClass" />
<?php __i("Association class"); ?>:&nbsp;
	<select name="assoc_class">
<?php
		$acs = $this->AssociationClassesModel->GetList();
		foreach ($acs as $c){
?>			<option value="<?php echo $c->ItemId(); ?>"<?php 
				if ($this->AssocClass())
					if ($this->AssocClass()->ItemId() == $c->ItemId())
						echo ' selected="true"';
			 ?>><?php echo $c; ?></option><?php		
		} 
?>	
	</select>
	<input type="submit" value="<?php __i('Apply'); ?>" />
</form>
<form id="<?php echo $this->Name() ?>_assoc" method="post" action="<?php $this->response->Url(); ?>">
	<input type="hidden" name="controller" value="<?php echo $this->Name(); ?>" />
	<input type="hidden" name="method" value="Link" />
<?php 
$this->renderArea($this->GetArea($this,"Suggestions"));
?>
</form>
<form id="<?php echo $this->Name() ?>_deassoc" method="post" action="<?php $this->response->Url(); ?>">
	<input type="hidden" name="controller" value="<?php echo $this->Name(); ?>" />
	<input type="hidden" name="method" value="Unlink" />
	<?php
	$this->renderArea($this->GetArea($this,"Assignments"));
	?>
	<div class="btn-holder"><input type="submit" value="<?php __i('Unlink'); ?>" /></div>
</form>
</div>
<script language="javascript">
	$("#<?php echo $this->Name(); ?>").areas_ajax_forms();
</script>
<?php		
		} else {
?>
<div>No page to associate!</div>
<?php			
		}
	}
	
/**
 * @see TJQueryWidget::linkCustomResources()
 */
	protected function linkCustomResources(){
		parent::linkCustomResources();
		$this->loadForms();
	}	
}
?>